<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/7/1
 */

class DbCxn
{
    // 连接到什么 DSN
    public static $dsn = [
        'test' => 'mysql:host=localhost;dbname=test', 'test', '123456',
        'tpch_small' => 'mysql:host=localhost;dbname=tpch_small', 'test', '123456',
    ];

    private static $db = [];

    final private function __construct()
    {
    }

    final private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function get($key)
    {
        var_dump(1222555);
        if (!isset(self::$dsn[$key])) {
            var_dump(1111111);
            throw new Exception("Unknown DSN: $key");
        }

        // 如果未连接则连接
        if (!isset(self::$db[$key])) {

            if (is_array(self::$dsn[$key])) {
                $c = new ReflectionClass('PDO');
                self::$db[$key] = $c->newInstanceArgs(self::$dsn[$key]);

            } else {
                self::$db[$key]= new PDO(self::$dsn[$key],'test','123456');
            }
        }
        // 返回这个连接
        return self::$db[$key];
    }
}

